package com.softserveinc.cinema;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class BookedSitsManager {

	private static final String TAG = BookedSitsManager.class.getSimpleName();

	private static final String BOOKED_FILE = "booked.ser";

	private static final String ANDROID_DATA_COM_SOFTSERVEINC = "/Android/data/com.softserveinc/";

	private BookedSitsManager() {
	}

	public static void saveBookedSits(final List<Boolean> list,
			final Context context) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String path = Environment.getExternalStorageDirectory()
						.getAbsolutePath() + ANDROID_DATA_COM_SOFTSERVEINC;

				FileOutputStream fos = new FileOutputStream(path + BOOKED_FILE);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(list);
				oos.close();

				Toast.makeText(context, "saved", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
		}
	}

	public static List<Boolean> getBookedSits() {
		// List<Boolean> booleans = new ArrayList<Boolean>();
		//
		// for (int i = 0; i < 7; i++) {
		// for (int j = 0; j < 6; j++) {
		// booleans.add(false);
		// }
		// }
		//
		// booleans.set(2, true);
		// booleans.set(5, true);
		// booleans.set(12, true);
		//
		// return booleans;

		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {

				String path = Environment.getExternalStorageDirectory()
						.getAbsolutePath() + ANDROID_DATA_COM_SOFTSERVEINC;

				FileInputStream fis = new FileInputStream(path + BOOKED_FILE);
				ObjectInputStream ois = new ObjectInputStream(fis);

				@SuppressWarnings("unchecked")
				List<Boolean> anotherList = (List<Boolean>) ois.readObject();
				ois.close();

				return anotherList;
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
		}

		return new ArrayList<Boolean>();
	}
}
